using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using FaceRecognitionDemo.Models;

namespace FaceRecognitionDemo.Data
{
    public class FaceDatabase : DbContext
    {
        public DbSet<Person> Persons { get; set; }
        public DbSet<Face> Faces { get; set; }
        public DbSet<FaceComparison> FaceComparisons { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=FaceRecognition.db");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置实体关系

            modelBuilder.Entity<Person>().HasKey(p => p.Id);
            modelBuilder.Entity<Person>().Property(p => p.Id).ValueGeneratedOnAdd();
            
            modelBuilder.Entity<Face>().HasKey(p => p.Id);
            modelBuilder.Entity<Face>().Property(p => p.Id).ValueGeneratedOnAdd();
            
            modelBuilder.Entity<FaceComparison>().HasKey(p => p.Id);
            modelBuilder.Entity<FaceComparison>().Property(p => p.Id).ValueGeneratedOnAdd();

            // Person与Face的一对多关系
            modelBuilder.Entity<Person>()
                .HasMany<Face>()
                .WithOne()
                .HasForeignKey(f => f.PersonId);

            // FaceComparison与Face的多对一关系
            modelBuilder.Entity<FaceComparison>()
                .HasOne<Face>()
                .WithMany()
                .HasForeignKey(fc => fc.ComparedFaceId);
            
            modelBuilder.Entity<FaceComparison>()
                .HasOne<Person>()
                .WithMany()
                .HasForeignKey(fc => fc.PersonId);
        }
    }
}